package cn.itcast.nio.c5;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import static cn.itcast.nio.c2.ByteBufferUtil.debugAll;

/**
 *
 */
@Slf4j
public class AioDemo1 {

    public static void main(String[] args) throws IOException {
        AsynchronousFileChannel s = AsynchronousFileChannel.open(Paths.get("data.txt"), StandardOpenOption.READ);
        ByteBuffer buffer = ByteBuffer.allocate(2);
        log.debug("begin...");
        s.read(buffer, 0, null, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {
                log.debug("read completed...{}",result);
                buffer.flip();
                debugAll(buffer);
            }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {
                log.debug("read failed");
            }
        });
        System.in.read();
    }
}
